<?php

namespace App\Models;
use Jenssegers\Mongodb\Eloquent\SoftDeletes;
use Rennokki\QueryCache\Traits\QueryCacheable;

class Comment extends Base
{
    use QueryCacheable, SoftDeletes;

    const TYPE_WORK = 1;
    const TYPE_MATERIAL = 2;
    const TYPE_SERVICE = 3;

    protected $fillable = ['user_id', 'entity_id', 'entity_user_id', 'type', 'parent_id', 'reply_to_id', 'reply_count', 'message'
        , 'refer_message', 'refer_user_id', 'comment_string_length'];

    public function child() {
        return $this->hasMany(Comment::class, 'parent_id');
    }

    public function reply() {
        return $this->belongsTo(Comment::class, 'reply_to_id')->withTrashed();
    }

    public function parent() {
        return $this->belongsTo(Comment::class, 'parent_id');
    }

    public function user() {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function entity_user() {
        return $this->belongsTo(User::class, 'entity_user_id');
    }

    public function is_star() {
        return $this->hasOne(Star::class, 'entity_id', 'id');
    }

    public function entity() {
        return $this->belongsTo(Article::class, 'entity_id');
    }

}
